extends Node2D
class_name Funnel

@onready var stateMachine = $StateMachine
@onready var laser:Sprite2D = $Laser
@onready var laserHitArea:Area2D = $LaserHitArea
@onready var laserCollisionShape:CollisionShape2D = $LaserHitArea/CollisionShape2D
@onready var hpBar:TextureProgressBar = $HP/TextureProgressBar

@export var speed = 100
@export var shootCastTime = 1.0
@export var shootValidTime = 0.3
@export var shootCooldown = 1.0
@export var randomMoveMinX = 10
@export var randomMoveMinY = 10
@export var randomMoveMaxX  =170
@export var randomMoveMaxY = 140
@export var maxHp = 100

var hp
var targetPos:Vector2 = Vector2.ZERO


func _ready():
	hp = maxHp
	laserCollisionShape.disabled = true
	stateMachine.addState("Move", self)
	
	updateHpUI()
	

func updateHpUI():
	hpBar.value = hp * 1.0 / maxHp


func _physics_process(delta):
	stateMachine.run(delta, self)


func _on_laser_hit_area_area_entered(area:Area2D):
	if area.name == "PlayerHitArea":
		area.onHit()


func onHit(damage):
	hp -= damage
	updateHpUI()
	
	if hp <= 0:
		queue_free()
	
